package com.oa.admin.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.joneying.common.annotation.CommonService;
import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.AuthenticationStepFileAddDTO;
import com.oa.admin.dto.CertificationAddDTO;
import com.oa.admin.dto.CertificationListDTO;
import com.oa.admin.entity.AuthenticationStepFile;
import com.oa.admin.entity.AuthenticationSteps;
import com.oa.admin.entity.CertificationFile;
import com.oa.admin.service.IAuthenticationStepFileService;
import com.oa.admin.service.IAuthenticationStepsService;
import com.oa.admin.service.ICertificationFileService;
import com.oa.admin.vo.CertificationDetailsVO;
import com.oa.admin.vo.CertificationListVO;
import com.oa.core.PageResult;
import com.oa.admin.entity.Certification;
import com.oa.admin.mapper.CertificationMapper;
import com.oa.admin.service.ICertificationService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.oa.core.utils.DateUtil;
import com.oa.core.utils.ExcelUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 认证管理 服务实现类
 * </p>
 *
 * @author syb123
 * @since 2019-01-22
 */
@Service
public class CertificationServiceImpl extends ServiceImpl<CertificationMapper, Certification> implements ICertificationService {

    private final CertificationMapper certificationMapper;

    @Autowired
    public CertificationServiceImpl(CertificationMapper certificationMapper) {
        this.certificationMapper = certificationMapper;
    }

    @Autowired
    private ICertificationFileService certificationFileService;

    @Autowired
    private IAuthenticationStepsService authenticationStepsService;

    @Autowired
    private IAuthenticationStepFileService authenticationStepFileService;

    @Override
    @CommonService
    public PageResult<Certification> findList(Integer pageNum, Integer pageSize) {
        if(pageNum == null){
            List<Certification> list = certificationMapper.findList();
            return new PageResult<>(list);
        }else{
            // 当前页，总条数 构造 page 对象
            Page<Certification> page = new Page<>(pageNum, pageSize);
            //查询
            List<Certification> list = certificationMapper.findList(page);
            return new PageResult<>(list);
        }
    }

    /**
    * 根据id查询
    */
    @Override
    @CommonService
    public Certification findCertificationById(String id){
        return certificationMapper.findById(id);
    }

    @Override
    @CommonService
    public PageResult<CertificationListVO> findCertificationList(CertificationListDTO certificationListDTO) {
        Page page = certificationListDTO.getPage();
        return new PageResult<>(certificationMapper.findCertificationList(page, certificationListDTO));
    }

    @Override
    @CommonService
    public CertificationDetailsVO findCertificationDetails(String id) {
        return certificationMapper.findCertificationDetails(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @CommonService
    public Resp add(CertificationAddDTO certificationAddDTO) {
        Certification certification = new Certification();
        BeanUtils.copyProperties(certificationAddDTO, certification);
        if(insert(certification)){
            if(certificationAddDTO.getCertificationFileAddDTO() != null && certificationAddDTO.getCertificationFileAddDTO().size() > 0){
                List<CertificationFile> certificationFiles = new ArrayList<>();
                certificationAddDTO.getCertificationFileAddDTO().forEach(d -> {
                    CertificationFile certificationFile = new CertificationFile();
                    d.setCertificationId(certification.getId());
                    BeanUtils.copyProperties(d, certificationFile);
                    certificationFiles.add(certificationFile);
                });
                certificationFileService.insertBatch(certificationFiles);
            }
            if(certificationAddDTO.getAuthenticationStepsAddDTOS() != null && certificationAddDTO.getAuthenticationStepsAddDTOS().size() > 0){
                certificationAddDTO.getAuthenticationStepsAddDTOS().forEach(d -> {
                    d.setCertificationId(certification.getId());
                    AuthenticationSteps authenticationSteps = new AuthenticationSteps();
                    BeanUtils.copyProperties(d, authenticationSteps);

                    authenticationStepsService.insert(authenticationSteps);

                    d.getAuthenticationStepFileAddDTO().forEach(a -> {
                        AuthenticationStepFile authenticationStepFiles = new AuthenticationStepFile();
                        BeanUtils.copyProperties(a, authenticationStepFiles);
                        authenticationStepFiles.setAuthenticationStepId(authenticationSteps.getId());
                        authenticationStepFileService.insert(authenticationStepFiles);
                    });
                });
            }
            return new Resp().success();
        }
        return new Resp().failure();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @CommonService
    public Resp edit(CertificationAddDTO certificationAddDTO) {
        Certification certification = new Certification();
        BeanUtils.copyProperties(certificationAddDTO, certification);
        if(updateById(certification)){

            if(certificationAddDTO.getCertificationFileAddDTO() != null && certificationAddDTO.getCertificationFileAddDTO().size() > 0){
                certificationFileService.delete(new EntityWrapper<CertificationFile>().eq("certification_id", certificationAddDTO.getId()));
                List<CertificationFile> certificationFiles = new ArrayList<>();
                certificationAddDTO.getCertificationFileAddDTO().forEach(d -> {
                    CertificationFile certificationFile = new CertificationFile();
                    BeanUtils.copyProperties(d, certificationFile);
                    certificationFile.setCertificationId(certification.getId());
                    certificationFiles.add(certificationFile);
                });
                certificationFileService.insertBatch(certificationFiles);
            }

            if(certificationAddDTO.getAuthenticationStepsAddDTOS() != null && certificationAddDTO.getAuthenticationStepsAddDTOS().size() > 0){
                authenticationStepFileService.delete(new EntityWrapper().where("authentication_step_id = (select id from admin_authentication_steps where certification_id = {0})", certification.getId()));
                authenticationStepsService.delete(new EntityWrapper<AuthenticationSteps>().eq("certification_id", certification.getId()));
                certificationAddDTO.getAuthenticationStepsAddDTOS().forEach(d -> {
                    AuthenticationSteps authenticationSteps = new AuthenticationSteps();
                    BeanUtils.copyProperties(d, authenticationSteps);
                    authenticationSteps.setCertificationId(certification.getId());

                    authenticationStepsService.insert(authenticationSteps);

                    d.getAuthenticationStepFileAddDTO().forEach(a -> {
                        AuthenticationStepFile authenticationStepFiles = new AuthenticationStepFile();
                        BeanUtils.copyProperties(a, authenticationStepFiles);
                        authenticationStepFiles.setAuthenticationStepId(authenticationSteps.getId());
                        authenticationStepFileService.insert(authenticationStepFiles);
                    });
                });
            }
            return new Resp().success();
        }
        return new Resp().failure();
    }

    @Override
    public void export(CertificationListDTO certificationListDTO, HttpServletResponse response) {
        List<CertificationListVO> certificationListVOS;

        if(certificationListDTO.getPage() != null){
            Page page = certificationListDTO.getPage();
            certificationListVOS = certificationMapper.findCertificationList(page, certificationListDTO);
        }else{
            certificationListVOS = certificationMapper.findCertificationList(new Page(), certificationListDTO);
        }
        String[] rows = {"id", "认证届数", "类别", "开始时间", "负责人", "认证结果", "认证状态"};
        try {
            ExcelUtil.downloadExcel("认证列表" + DateUtil.getDayMonth(), rows, certificationListVOS, "认证列表" + DateUtil.getDayMonth()+".xls", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
